using System.ComponentModel.DataAnnotations;
using MediatR;
using SmartTracking.ErrorCode;

namespace BaseData.WriteApi.Appcontext.Command.Sheep_Records
{
    /// <summary>
    /// 羊只档案更新命令
    /// </summary>
    public class UpdateSheepRecordCommand : IRequest<ApiResult<bool>>
    {
        /// <summary>
        /// 羊只档案ID
        /// </summary>
        [Required]
        public int SheepId { get; set; }

        /// <summary>
        /// 可视化耳号
        /// </summary>
        [Required]
        public string VisibleNumber { get; set; } = string.Empty;
        
        /// <summary>
        /// 电子耳号
        /// </summary>
        public string ElectronNumber { get; set; } = string.Empty;
        
        /// <summary>
        /// 品种
        /// </summary>
        public int Variety { get; set; }
        
        /// <summary>
        /// 性别
        /// </summary>
        public int Gender { get; set; }
        
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime DateofBirth { get; set; }
        
        /// <summary>
        /// 月龄
        /// </summary>
        public int Ageinmonths { get; set; }
        
        /// <summary>
        /// 生长阶段
        /// </summary>
        public int Growthstage { get; set; }
        
        /// <summary>
        /// 栋舍
        /// </summary>
        public int Building { get; set; }
        
        /// <summary>
        /// 栏位
        /// </summary>
        public string Column { get; set; } = string.Empty;
        
        /// <summary>
        /// 羊只类型
        /// </summary>
        public int TypeofSheep { get; set; }
        
        /// <summary>
        /// 基因等级
        /// </summary>
        public int GeneRank { get; set; }
        
        /// <summary>
        /// 生产等级
        /// </summary>
        public int ProductionGrade { get; set; }
        
        /// <summary>
        /// 销售登记
        /// </summary>
        public int SalesLevel { get; set; }
        
        /// <summary>
        /// 繁殖状态
        /// </summary>
        public int Reproductivestatus { get; set; }
        
        /// <summary>
        /// 是否妊检
        /// </summary>
        public int PrenatalCheckup { get; set; }
        
        /// <summary>
        /// 繁殖状态天数
        /// </summary>
        public int ReproductiveStatusDay { get; set; }
        
        /// <summary>
        /// 出生重量 (单位: kg)
        /// </summary>
        public int BirthWeight { get; set; }
        
        /// <summary>
        /// 断奶重量 (单位: kg)
        /// </summary>
        public int WeaningWeight { get; set; }
        
        /// <summary>
        /// 断奶日期
        /// </summary>
        public DateTime WeaningDate { get; set; }
        
        /// <summary>
        /// 断奶日龄
        /// </summary>
        public int WeaningAge { get; set; }
        
        /// <summary>
        /// 父耳号
        /// </summary>
        public string FatherNumber { get; set; } = string.Empty;
        
        /// <summary>
        /// 母耳号
        /// </summary>
        public string MotherNumber { get; set; } = string.Empty;
        
        /// <summary>
        /// 在场状态
        /// </summary>
        public int PresenceStatus { get; set; }
        
        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; } = string.Empty;

        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <returns>验证结果</returns>
        public bool Validate(out string errorMessage)
        {
            errorMessage = string.Empty;

            if (SheepId <= 0)
            {
                errorMessage = "羊只档案ID必须大于0";
                return false;
            }

            if (string.IsNullOrWhiteSpace(VisibleNumber))
            {
                errorMessage = "可视化耳号不能为空";
                return false;
            }

            if (DateofBirth > DateTime.Now)
            {
                errorMessage = "出生日期不能晚于当前日期";
                return false;
            }

            if (WeaningDate > DateTime.Now)
            {
                errorMessage = "断奶日期不能晚于当前日期";
                return false;
            }

            if (BirthWeight <= 0 || BirthWeight > 500)
            {
                errorMessage = "出生重量必须大于0且小于500kg";
                return false;
            }

            if (WeaningWeight <= 0 || WeaningWeight > 500)
            {
                errorMessage = "断奶重量必须大于0且小于500kg";
                return false;
            }

            if (GeneRank < 1 || GeneRank > 5)
            {
                errorMessage = "基因等级必须在1-5之间";
                return false;
            }

            if (Gender < 1 || Gender > 2)
            {
                errorMessage = "性别必须为1(雄性)或2(雌性)";
                return false;
            }

            if (PresenceStatus < 1 || PresenceStatus > 3)
            {
                errorMessage = "在场状态必须为1(在场)、2(离场)或3(死亡)";
                return false;
            }

            if (PrenatalCheckup < 0 || PrenatalCheckup > 1)
            {
                errorMessage = "是否妊检必须为0(未妊检)或1(已妊检)";
                return false;
            }

            return true;
        }
    }
}